package com.chdwmp.product.controller;

import com.chdwmp.product.bean.CommonResponse;
import com.chdwmp.product.bean.Product;
import com.chdwmp.product.bean.app.Activation;
import com.chdwmp.product.bean.app.AgentProductVo;
import com.chdwmp.product.bean.app.EquipmentUpdate;
import com.chdwmp.product.bean.backend.AgentVo;
import com.chdwmp.product.service.AppService;
import com.chdwmp.product.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: LLL
 * @Date: 2021/3/13 21:10
 * @Description: app交互controller
 */
@Api(description = "app和服务器交互模块", tags = {"app和服务器交互模块接口控制层"})
@RequestMapping("/app")
@RestController
public class AppController {

    @Autowired
    AppService appService;

    /**
     * APP主动申请获取服务器地址和序列号等信息
     * @param imei
     * @param phone
     * @return
     * @throws Exception
     */
    @GetMapping("/getAppSystem/{imei}/{phone}")
    @ApiOperation(value = "APP主动申请获取服务器地址和序列号等信息", notes = "APP主动申请获取服务器地址和序列号等信息", httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getAppSystem(@ApiParam(name = "imei", value = "手机imei", required = true)@PathVariable String imei,
                                           @ApiParam(name = "phone", value = "手机号")@PathVariable String phone) throws Exception {

        return new CommonResponse(appService.getAppSystemInfo(imei,phone));
    }

    /**
     * 设备保活
     * @param imei
     * @param password
     * @param status 设备状态 1-不在线 2-在线(无连接) 3-在线(p2p) 4-在线(RLY)
     * @return boolean
     * @throws Exception
     */
    @GetMapping("/getAppSystemInfo/{imei}/{serialNumber}/{password}/{status}")
    @ApiOperation(value = "设备保活", notes = "设备保活",httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getAppSystemInfo(@ApiParam(name = "imei", value = "手机imei", required = true)@PathVariable String imei,
                                           @ApiParam(name = "password", value = "密码", required = true)@PathVariable String password,
                                           @ApiParam(name = "serialNumber", value = "密码", required = true)@PathVariable String serialNumber,
                                           @ApiParam(name = "status", value = "设备状态 1-不在线 2-在线(无连接) 3-在线(p2p) 4-在线(RLY)", required = true)@PathVariable String status, HttpServletRequest request) throws Exception {

        String ipAddr = StringUtil.getIpAddress(request);
        return new CommonResponse(appService.getKeepLived(imei,serialNumber,password,status,ipAddr));
    }

    /**
     *设备获取信息
     * @param imei
     * @return
     * @throws Exception
     */
    @GetMapping("/getEquipment/{imei}")
    @ApiOperation(value = "设备获取信息", notes = "设备获取信息",httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getEquipment(@ApiParam(name = "imei", value = "手机imei", required = true)@PathVariable String imei) throws Exception {

        return new CommonResponse(appService.getEquipment(imei));
    }

    /**
     * 获取设备是否在线的接⼝
     * @param imei
     * @return string 设备状态 1-不在线 2-在线(无连接) 3-在线(p2p) 4-在线(RLY)
     * @throws Exception
     */
    @GetMapping("/getUserAppCurrentStatus/{imei}")
    @ApiOperation(value = "获取设备是否在线的接⼝", notes = "获取设备是否在线的接⼝",httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getUserAppCurrentStatus(@ApiParam(name = "imei", value = "手机imei", required = true)@PathVariable String imei) throws Exception {

        return new CommonResponse(appService.getUserAppCurrentStatus(imei));
    }

    /**
     * 获取所有产品
     * @return List<Product>
     * @throws Exception
     */
    @GetMapping("/getAllProducts")
    @ApiOperation(value = "获取所有产品", notes = "获取所有产品",httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getProductList() throws Exception {

        return new CommonResponse(appService.getProductList());
    }

    /**
     * 获取用户购买产品
     * @return List<Product>
     * @throws Exception
     */
    @GetMapping("/getUserProducts/{imei}")
    @ApiOperation(value = "获取用户购买产品", notes = "获取用户购买产品",httpMethod = "GET", response = CommonResponse.class)
    public CommonResponse getUserProducts(@ApiParam(name = "imei", value = "手机imei", required = true)@PathVariable String imei) throws Exception {

        return new CommonResponse(appService.getUserProducts(imei));
    }

    /**
     * 设备激活
     * @param activation
     * @return
     * @throws Exception
     */
    @PostMapping("/activation")
    @ApiOperation(value = "设备激活", notes = "设备激活",httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse isActivation(@RequestBody Activation activation) throws Exception {

        return new CommonResponse(appService.isActivation(activation));
    }


    /**
     * 更新设备信息（相机参数）
     * @param equipmentUpdate
     * @return
     * @throws Exception
     */
    @PostMapping("/updateEquipment")
    @ApiOperation(value = "更新设备信息（相机参数）", notes = "更新设备信息（相机参数）",httpMethod = "POST", response = CommonResponse.class)
    public CommonResponse getEquipment(@RequestBody EquipmentUpdate equipmentUpdate) throws Exception {
        appService.updateEquipment(equipmentUpdate);
        return new CommonResponse();
    }

    /**
     * 获取所有产品列表
     * @return
     * @throws Exception
     */
    @GetMapping("/getProductList")
    @ApiOperation(value = "获取所有产品列表", notes = "获取所有产品列表",httpMethod = "GET", response = Product.class)
    public CommonResponse<Product> getALlProductList() throws Exception {

        return new CommonResponse(appService.getALlProductList());
    }

    /**
     * 获取供应商购买的产品
     * @param name
     * @return
     * @throws Exception
     */
    @GetMapping("/getAgentProductList/{name}")
    @ApiOperation(value = "获取供应商购买的产品", notes = "获取供应商购买的产品",httpMethod = "GET", response = AgentProductVo.class)
    public CommonResponse<AgentProductVo> getAgentProductList(@PathVariable  String name) throws Exception {

        return new CommonResponse(appService.getAgentProductList(name));
    }

    /**
     * 获取供应商列表
     * @return
     * @throws Exception
     */
    @GetMapping("/getAllAgentList")
    @ApiOperation(value = "获取供应商列表", notes = "获取供应商列表",httpMethod = "GET", response = AgentVo.class)
    public CommonResponse<AgentVo> getAllAgentList() throws Exception {

        return new CommonResponse(appService.getAllAgentList());
    }

}
